/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.service.impl;

import com.google.inject.Inject;

import kiff.dao.CoreDAO;
import kiff.dao.ForumAclDAO;
import kiff.dao.ForumDAO;
import kiff.entity.Forum;
import kiff.entity.ForumAcl;
import kiff.exception.DataLayerException;
import kiff.exception.ServiceLayerException;
import kiff.security.Principal;
import kiff.service.ForumAclService;

/**
 * An implementation of the ForumAclService interface.
 * @author Adam
 * @version $Id$
 * 
 * Created on Dec 1, 2008 at 5:57:50 PM 
 */
public class ForumAclServiceImpl extends AbstractService<ForumAcl> implements ForumAclService {
	/**
	 * Forum Data Access Object.
	 */
	private ForumDAO forumDAO;

	/**
	 * {@inheritDoc}
	 * @see kiff.service.ForumAclService#findByOwner(kiff.security.Principal)
	 */
	public ForumAcl findByOwner(final Principal owner) throws ServiceLayerException {
		try {
			if (owner != null && ((Forum) owner).getId() != null) {
				Forum forum = forumDAO.loadById(((Forum) owner).getId());
				return (ForumAcl) ((ForumAclDAO) dao).findByOwner(forum);
			} else {
				return (ForumAcl) ((ForumAclDAO) dao).findByName(ForumAcl.DEFAULT_NAME);
			}
		} catch (DataLayerException dataLayerException) {
            throw new ServiceLayerException(dataLayerException);
        }
	}

	/**
	 * {@inheritDoc}
	 * Note: This method can be removed after upgrading to Guice 2.0.
	 * @see kiff.service.impl.AbstractService#setDao(kiff.dao.CoreDAO)
	 */
	@Override @Inject(optional = true)
	public void setDao(final CoreDAO<ForumAcl> dao) {
		this.dao = dao;
	}

	/**
	 * Sets forumDAO.
	 * @param forumDAO the forumDAO to set.
	 */
	@Inject
	public void setForumDAO(final ForumDAO forumDAO) {
		this.forumDAO = forumDAO;
	}
}
